package com.example.ui_view;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class ImageSwitcherActivity extends AppCompatActivity {
        int[] pic = new int[]{R.mipmap.img01,R.mipmap.img02,R.mipmap.img03,R.mipmap.img04,R.mipmap.img05,R.mipmap.img06};
        private ImageSwitcher imageSwitcher;
        private Gallery gallery;
        float donwX,upX;
        int currenIndex = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_switcher);
        imageSwitcher = findViewById(R.id.imageSwitcher);
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(ImageSwitcherActivity.this);
                imageView.setImageResource(pic[2]);
                return imageView;
            }
        });
        imageSwitcher.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == event.ACTION_DOWN){
                    donwX = event.getX();
                    return true;
                }else if(event.getAction() == event.ACTION_UP){
                    upX = event.getX();
                    if (donwX - upX>10){
                        currenIndex = currenIndex - 1;
                        if(currenIndex<0){
                            currenIndex = pic.length - 1;
                        }
                        imageSwitcher.setImageResource(pic[currenIndex]);
                    }
                    if (upX - donwX>10){
                        currenIndex = currenIndex + 1;
                        if(currenIndex == pic.length){
                            currenIndex = 0;
                        }
                        imageSwitcher.setImageResource(pic[currenIndex]);
                    }
                    return true;
                }
                return false;
            }
        });
        gallery = findViewById(R.id.gallery);
        BaseAdapter adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return pic.length;
            }

            @Override
            public Object getItem(int position) {
                return pic[position];
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView imageView;
                if (convertView == null){
                    imageView = new ImageView(ImageSwitcherActivity.this);
                }else {
                    imageView =(ImageView) convertView;
                }
                imageView.setImageResource(pic[position]);
                return imageView;
            }
        };
        gallery.setAdapter(adapter);
    }
}
